package wh.coach._stream;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.google.common.collect.Lists;

/**
 * @author hangwen
 * @date 2023/7/1
 */
public class Main {
    public static void main(String[] args) {
        //构建stream的方式
        //1.Collection.stream();
        //2.Stream.of(Obj),Stream.of(Array)
        String[] strs = new String[]{"1", "2", "3"};
        final Stream<String> stream = Stream.of(strs);
        //3.其他
        //IntStream 可以用各种方式构建int序列

        List<String> strList = prepareUpperStrList();

        //map
        //
        // 输出对应的小写
        List<String> lowerList = strList.stream().map(String::toLowerCase).collect(Collectors.toList());
        //java 16+可以直接toList，输出一个不可变的List,性能优于前者
        //List<String> lowerList = strList.stream().map(String::toLowerCase).toList()
        System.out.println("map,toLower:" + lowerList);

        //filter
        //
        //从52个字母里过滤出小写
        strList = prepareStrList();
        lowerList = strList.stream().filter(str -> Character.isLowerCase(str.charAt(0))).collect(Collectors.toList());
        System.out.println("filter,lower:" + lowerList);

        //lowerList是新的集合，stream只是读取原始集合，不会对其做任何修改

        //排序
        //约束：
        //1.如果a>b,b>c 那么a>c
        //
        //降序排列
        strList = prepareUpperStrList();
        //该方法直接对strList进行修改
        Collections.reverse(strList);
        System.out.println("Collections.reverse:" + strList);

        //该方法直接对strList进行修改
//        Collections.sort(strList, Comparator.naturalOrder());
//        Collections.sort(strList, Comparator.reverseOrder());
//        System.out.println("Collections.sort:" + strList);

        strList = prepareUpperStrList();
        List<String> reversed = strList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        System.out.println("stream().sorted:" + reversed);
        System.out.println("origin:" + strList);

        //skip limit

        //过滤 转换 排序 组合使用

        //给定一组序列，去重

        //终结操作——只有执行了终结操作，stream指定的各类规则器才会真正执行

    }

    private static List<String> prepareStrList() {
        List<String> strList = Lists.newArrayListWithExpectedSize(52);
        for (int i = 65; i < 91; i++) {
            strList.add((char) i + "");
            strList.add((char) (i + 32) + "");
        }

        return strList;
    }

    private static List<String> prepareUpperStrList() {
        List<String> strList = Lists.newArrayListWithExpectedSize(26);
        for (int i = 65; i < 91; i++) {
            strList.add((char) i + "");
        }

        return strList;
    }
}
